package com.xzc.java8.B_Stream.i_practice;

import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

public class A_P2 {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        //找出所有2011年的交易，并通过金额排序
        List<Transaction> out1 = transactions.stream().filter(t -> t.getYear() == 2011).sorted(comparing(Transaction::getValue)).collect(toList());
        System.out.println(out1);

        System.out.println(2);
        //商人们在哪些城市工作
        List<String> out2 = transactions.stream().map(d -> d.getTrader().getCity()).distinct().collect(toList());
        System.out.println(out2);
        Set<String> out2_1 = transactions.stream().map(d -> d.getTrader().getCity()).collect(toSet());
        System.out.println(out2_1);

        System.out.println(3);
        //找出所有在剑桥的商人，并按照他们的名字排序 ?
//        transactions.stream().filter(d->d.getTrader().getCity().equals("Cambridge")).sorted(a->a.getTrader().getName());
        List<Trader> out3 = transactions.stream().map(Transaction::getTrader).filter(d -> d.getCity().equals("Cambridge")).distinct().sorted(comparing(Trader::getName)).collect(toList());
        System.out.println(out3);

        System.out.println(4);
        //返回一个字符串，其由所有商人的名字（字母排序）所构成 ?
//        transactions.stream().
        String out4 = transactions.stream().map(d -> d.getTrader().getName()).distinct().sorted().reduce("", (n1, n2) -> n1 + n2);
        System.out.println(out4);
        //性能更高，内部用StringBuilder
        String out4_1 = transactions.stream().map(d -> d.getTrader().getName()).distinct().sorted().collect(joining());
        System.out.println(out4_1);

        System.out.println(5);
        //找出任意一个在米兰的商人
        Optional<Trader> out5 = transactions.stream().map(d -> d.getTrader()).filter(d -> d.getCity().equals("Milan")).findAny();
        out5.ifPresent(System.out::println);
        //是否有米兰商人
        boolean out5_1 = transactions.stream().anyMatch(d -> d.getTrader().getCity().equals("Milan"));
        System.out.println(out5_1);

        System.out.println(6);
        //打印出所有居住在剑桥的商人的交易单金额
        transactions.stream().filter(d -> d.getTrader().getCity().equals("Cambridge")).map(Transaction::getValue).forEach(System.out::println);

        System.out.println(7);
        //交易单最高金额是多少?
//        Optional<Integer> out7 = transactions.stream().flatMap(d -> (Integer) d.getValue()).max(Integer::max);
        Optional<Integer> out7 = transactions.stream().map(Transaction::getValue).reduce(Integer::max);
        out7.ifPresent(System.out::println);

        System.out.println(8);
        //找出金额最小的交易单
        Optional<Transaction> out8 = transactions.stream().reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2);
        out8.ifPresent(System.out::println);
        Optional<Transaction> out8_1 = transactions.stream().min(comparing(Transaction::getValue));
        out8_1.ifPresent(System.out::println);
    }
}
